package cn.tedu._07springaop.egaop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import org.springframework.stereotype.Component;

/**
 * @Author Justin Parker
 * @Date 2024/8/15 0015 11:54
 * @Version 1.0
 */
@Aspect
@Component
public class CartAspect {

//    @Before(value ="execution(public * cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
//    public void beforeMethod(JoinPoint joinPoint){
//
//        System.out.println("开始添加购物车");
//    }
//
//    @After(value ="execution(public * cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
//    public void afterMethod(JoinPoint joinPoint){
//        System.out.println("操作完成");
//    }
//
//    @AfterReturning(value = "execution(public * cn.tedu._07springaop.egaop.CartServiceImpl.*(..))",returning = "r")
//    public void afterReturningMethod(JoinPoint joinPoint,Object r) {
//
//        if (r) {
//            System.out.println("添加购物车成功");
//        } else {
//            System.out.println("添加购物车失败");
//        }
//    }

    @Around(value ="execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
    public boolean aroundMethod(ProceedingJoinPoint joinPoint) {
        boolean result = false;
        try {
            System.out.println("开始添加购物车");
            result = (boolean) joinPoint.proceed();
            if (result) {
                System.out.println("添加购物车成功");
            } else {
                System.out.println("添加购物车失败");
            }
        } catch (Throwable e) {
           throw new RuntimeException(e);
        }finally {
            System.out.println("操作完成");
        }
        return result;
    }
}
